package com.lagou.bigdata.task04;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {

    public static ArrayList<Student> students = new ArrayList<>();
    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            printInfo();
            int index = sc.nextInt();

            switch (index) {
                case 1:
                    // 遍历
                    viewAllStudent();
                    break;
                case 2:
                    // 添加
                    addStudent();
                    break;

                case 3:
                    // 删除
                    deleteStudent();
                    break;
                case 4:
                    // 修改
                    modifyStudent();
                    break;
                case 5:
                    // 查找
                    searchStudent();

                    break;
                case 6:
                    // 退出
                    System.out.println("谢谢使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("输入错误,正确值 1-6");
            }
        }


    }


    public static void printInfo() {
        System.out.println("------------------------菜单------------------------");
        System.out.println("1-遍历用户 2-增加用户 3-删除用户 4-修改用户 5-查找用户 6-退出");
    }


    // 遍历
    public static void viewAllStudent() {
        for (Student stu : students) {
            System.out.println("id:" + stu.getId() + ",姓名：" + stu.getName() + ",年龄:" + stu.getAge());
        }
    }


    // 获取学生对象
    public static Student getStudent() {
        System.out.println("请输入学生id:");
        int id = sc.nextInt();

        for (Student stu: students) {
            if (stu.getId() == id) {
                return stu;
            }
        }
        return null;
    }


    // 添加学生
    public static void addStudent() {



        System.out.println("请输入学生id:");
        int id = sc.nextInt();
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();

        Student student = new Student(id, name, age);
        if (students.contains(student)) {
            System.out.println("学生已存在");
        } else {
            students.add(student);
        }

    }

    // 修改
    public static void modifyStudent(){
        Student student = getStudent();

        if (null == student) {
            System.out.println("学生不存在");
        } else {
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();

            student.setAge(age);
            System.out.println("修改成功");
        }
    }

    // 查看找用户
    public static void searchStudent(){
        Student student = getStudent();

        if (null == student) {
            System.out.println("查无此人");
        } else{
            System.out.println("id:" + student.getId() + ",姓名：" + student.getName() + ",年龄:" + student.getAge());
        }
    }

    // 删除用户
    public static void deleteStudent() {
        Student student = getStudent();

        if (null == student) {
            System.out.println("没有此人");
        } else{
            students.remove(student);
            System.out.println("删除成功");
        }

    }



}
